package v1

import (
	"auth/authentication"
	"auth/bll"
	"auth/model"
	"auth/utils"
	"github.com/gin-gonic/gin"
)

func (a *user) appLogin(c *gin.Context) {
	var (
		u   = &model.UserAppLoginRequest{}
		out *model.UserAppLoginResponse
		err error
	)

	if err = c.ShouldBindJSON(u); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.User.AppLogin(c.Request.Context(), u); err != nil {
		c.Error(err)
		return
	}

	utils.ResponseOK(c, out)
}

func (a *user) appUserInfo(c *gin.Context) {
	var (
		out *model.UserAppInfoResponse
		err error
	)

	id, err := authentication.ContextUserID(c.Request.Context())
	if err != nil {
		return
	}

	if out, err = bll.User.AppUserInfo(c.Request.Context(), id); err != nil {
		c.Error(err)
		return
	}

	utils.ResponseOK(c, out)
}

// replacePhone 换手机号
func (a *user) replacePhone(c *gin.Context) {
	var (
		in  = &model.AppUserPhoneReplaceRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.ReplacePhone(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

// setPassword 设置密码
func (a *user) setPassword(c *gin.Context) {
	var (
		in  = &model.AppUserSetPasswordRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.SetPassword(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

// updatePassword 修改密码
func (a *user) updatePassword(c *gin.Context) {
	var (
		in  = &model.AppUserUpdatePasswordRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.UpdatePassword(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

// appUpdate 用户信息修改
func (a *user) appUpdate(c *gin.Context) {
	var (
		in  = &model.AppUserUpdateRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.AppUpdate(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

// forgotPassword 忘记密码
func (a *user) forgotPassword(c *gin.Context) {
	var (
		in  = &model.AppForgotPasswordRequest{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if err = bll.User.ForgotPassword(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
	return
}

// 微信用户绑定
func (a *user) wxAccountBind(c *gin.Context) {
	var (
		in  = &model.ThirdPartyIDBindRequest{}
		out *model.UserAppLoginResponse
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.User.WxAccountBind(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

// 苹果帐号绑定
func (a *user) appleAccountBind(c *gin.Context) {
	var (
		in  = &model.ThirdPartyIDBindRequest{}
		out *model.UserAppLoginResponse
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.User.AppleAccountBind(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}
